<?php require "vuelta.config.php" ?>
<?php require "vuelta_queries.php" ?>
<!DOCTYPE html>
<html>
<head>
  <title>Vuelta Tourspel - Extra renner opties</title>
  <meta charset="utf-8">
  <script type="text/javascript"></script>
  <script type="text/javascript" src="js/bsn.AutoSuggest_2.1.3_comp.js" charset="utf-8"></script>
  <?php Vuelta::css('css/generic.css');?>
  <?php Site::css('header');?>
  <?php Site::css('login');?>
  <?php Vuelta::css('css/select.css');?>
  <?php Vuelta::css('css/autosuggest.css');?>
</head>

<body>
  <?php Site::load('header'); ?>
  <?php Site::load('login'); ?>
  <div class="content">

    <?php 
      if(!is_logged_in()) {
?>      <p>Je moet ingelogd zijn om deze pagina te bekijken</p> <?php
      }
      else {
        // logged in
        
        $db = Vuelta::get_database_connection();

        $runners = get_runners($db);
        $runners_rev = get_runners_rev($db);
        $chosen = get_chosen($db);

        function d_check(&$d) {
          global $runners_rev;
          global $chosen;

          $d['name'] = strtolower(trim($d['value'])); 

          if($d['name'] == '') {
            $d['tag'] = 'empty';
          }
          elseif(!isset($runners_rev[$d['name']])) {
            $d['tag'] = 'no-runner';
          }
          else {
            //not empty and is a runner
            $d['id'] = $runners_rev[$d['name']];
            $d['tag'] = 'valid';
          }
        }

        $dchosen = Array();
        for($i = 0; $i < 30; $i++) {
          $dchosen[$i] = Array('value' => "");
        }

        if(pempty()) {
          // get from database
          for($i = 0; $i < 30; $i++) {
            if(isset($chosen[$i])) {
              $dchosen[$i]['value'] = $chosen[$i];
            }
          }
        }
        else{
          // get from post
          for($i = 0; $i < 30; $i++) {
            $dchosen[$i]['value'] = pget('chosen_'.$i);
          }
        }

        for($i = 0; $i < 30; $i++) {
          d_check($dchosen[$i]);
        }

        // check for double in chosen
        $count = Array();
        for($i = 0; $i < 30; $i++) {
          $cur = &$dchosen[$i];
          if(isset($cur['id'])) {
            $id = $cur['id'];

            if(isset($count[$id])) {
              $cur['tag'] = 'double';
              $count[$id]++;
            }
            else {
              $count[$id] = 1;
            }
          }
        }

        if(!pempty()) {
          //update database
          
          unset_chosen($db);

          // set all correct runners
          for($i = 0; $i < 30; $i++) {
            if(isset($dchosen[$i]['id'])) {
              set_chosen($db, $dchosen[$i]['id'], $i);
            }
          }
        }
        
        $db->close();

        //if(count($chosen) != 30) { 
?>        <p>Het is de bedoeling dat je eerst een <a href="choose.php">selectie</a> van 30 renners maakt</p>
          <p>Je hebt op het moment <?=count($chosen)?>/30 renners al gekozen</p>

<?php   //}

        
        function d_tag_to_message($tag) {
          $convert = Array(
            'empty' => 'leeg',
            'no-runner' => 'renner niet gevonden',
            'not-chosen' => 'renner niet in selectie', 
            'valid' => 'goedgekeurd');
          return isset($convert[$tag]) ? $convert[$tag] : $tag;
        }
        ?>
        
        <form class="thin-border" action="choose.php" method="POST">
          <small style="display:none;"><input id="hiddenid" disabled="disabled" type="text" /></small>
           <table>
            <caption>selectie</caption>
            <col width="40px" />
            <col />
            <col width="180px"/>
            <thead>
            </thead>
            <tfoot>
            </tfoot>
            <tbody>
<?php       for($i = 0; $i < 30; $i++) {
              $cur = &$dchosen[$i];
              $value = $cur['value'];
              $tag = $cur['tag'];
              $message = d_tag_to_message($tag);
?>          <tr valign="baseline">
              <td><?=$i+1?></td>
              <td><input type="text" id="chosen_<?=$i?>" name="chosen_<?=$i?>" value="<?=$value?>" /></td>
              <td align="center" class="<?=$tag?>"><?=$message?></td>
            </tr>
<?php       }
?>          </tbody>
          </table>
          <p>LET OP! Renners die niet mee doen aan de vuelta (no-runner) kunnen <em>niet</em> worden opgeslagen!</p>
          <p>Indien u van mening bent dat dit niet klopt kunt u contact op nemen met all@mightydev.nl</p>
          <button>Verzend</button>
        </form>
<script type="text/javascript">
  var options = {
    script:"./json.php?",
    varname:"input",
    json:true,
    shownoresults:false,
    maxresults:0,
    callback: function (obj) { document.getElementById('hiddenid').value = obj.id; }
  };
  <?php for($i = 0; $i < 30 ; $i++) { ?>
    var as_json_chosen_<?=$i?>= new bsn.AutoSuggest('chosen_<?=$i?>', options);
  <?php } ?>


  </script>
<?php   } 
?></div>
</body>
</html>
